package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginCotroller {
    @Autowired
    private EmpService empService;

    @PostMapping
    public Result login(@RequestBody Emp emp) {
        Emp emp1 = empService.verifyEmp(emp);
        if (emp1 != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", emp1.getId());
            claims.put("username", emp1.getUsername());
            claims.put("name", emp1.getName());
            String jwt = JwtUtils.getJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或者密码错误!");
    }
}
